/**
 *
 */
package com.goodsogood.union.auth.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goodsogood.service.RespCreate;
import com.goodsogood.service.union.user.thrift.UserInfo;
import com.goodsogood.service.union.user.thrift.UserInfoResp;
import com.goodsogood.tool.result.ResponseResult;
import com.goodsogood.union.auth.conf.LogConfiger;
import com.goodsogood.union.auth.service.SyncService;
import com.goodsogood.union.auth.thrfit.client.UserClient;
import com.goodsogood.union.auth.util.Md5PwdEncoder;

/**
 * @author 董国超
 */
@Service("syncService")
public class SyncServiceImpl implements SyncService {
    
    private static final Logger logger = LoggerFactory.getLogger(LogConfiger.APP);
    @Autowired
    private UserClient userClient;

    @Override
    public ResponseResult idCardPwdCheck(Map<String, String> filter) {
        RespCreate id = userClient.getUserIdByIdCard(filter.get("userIdCard"));
        if(id==null){
            logger.error("idCardPwdCheck.getUserIdByIdCard is fail, filter is {}, respCreate is null", filter);
            return ResponseResult.failInstance("检测失败");
        }
        if(id.getCode()!=0){
            logger.error("idCardPwdCheck.getUserIdByIdCard is fail, filter is {}, respCreate is {}", filter, id);
            return ResponseResult.instance(id.getCode(), id.getMessage());
        }
        UserInfoResp resp = userClient.getUserInfo(id.getId());
        if(resp==null){
            logger.error("idCardPwdCheck.getUserInfo is fail, filter is {}, UserInfoResp is null", filter);
            return ResponseResult.failInstance("检测失败");
        }
        if(resp.getCode()!=0){
            logger.error("idCardPwdCheck.getUserInfo is fail, filter is {}, UserInfoResp is {}", filter, resp);
            return ResponseResult.instance(resp.getCode(), resp.getMessage());
        }
        UserInfo info = resp.getData();
        //pwd compare
        String pwd = new Md5PwdEncoder().encodePassword(filter.get("pwd"), info.getPasswordSalt());
        if(pwd.equalsIgnoreCase(info.getPassword())){
            return ResponseResult.successInstance();
        }else{
            return ResponseResult.instance(201003);
        }
    }

    @Override
    public ResponseResult phonePwdCheck(Map<String, String> filter) {
        RespCreate id = userClient.getUserIdByPhone(filter.get("userPhone"));
        if(id==null){
            logger.error("phonePwdCheck.getUserIdByPhone is fail, filter is {}, respCreate is null", filter);
            return ResponseResult.failInstance("检测失败");
        }
        if(id.getCode()!=0){
            logger.error("phonePwdCheck.getUserIdByPhone is fail, filter is {}, respCreate is {}", filter, id);
            return ResponseResult.instance(id.getCode(), id.getMessage());
        }
        UserInfoResp resp = userClient.getUserInfo(id.getId());
        if(resp==null){
            logger.error("phonePwdCheck.getUserInfo is fail, filter is {}, UserInfoResp is null", filter);
            return ResponseResult.failInstance("检测失败");
        }
        if(resp.getCode()!=0){
            logger.error("phonePwdCheck.getUserInfo is fail, filter is {}, UserInfoResp is {}", filter, resp);
            return ResponseResult.instance(resp.getCode(), resp.getMessage());
        }
        UserInfo info = resp.getData();
        //pwd compare
        String pwd = new Md5PwdEncoder().encodePassword(filter.get("pwd"), info.getPasswordSalt());
        if(pwd.equalsIgnoreCase(info.getPassword())){
            return ResponseResult.successInstance();
        }else{
            return ResponseResult.instance(201003);
        }
    }
}
